Skip to content

feat(instrumentation-http): provide http.request.header.<key> at server span creation time#6396

Merged
trentm merged 6 commits intoopen-telemetry:mainfrom
vitorvasc:instr-http_header-capture
Mar 2, 2026
Merged

feat(instrumentation-http): provide http.request.header.<key> at server span creation time#6396
trentm merged 6 commits intoopen-telemetry:mainfrom
vitorvasc:instr-http_header-capture

Conversation

@vitorvasc
Copy link
Copy Markdown
Member

Which problem is this PR solving?

The HTTP semconv spec states that http.request.header.<key> SHOULD be provided at span creation time, as it can be important for making sampling decisions. The current behavior is that instrumentation-http only captures server hequest headers after span creation inside the context.with() callback.

Fixes #6393

Short description of the changes

  • Refactored headerCapture in utils.ts to return an Attributes object instead of calling span.setAttribute() directly.
  • Moved server request header capture in http.ts to before _startHttpSpan, merging the captured attributes into spanOptions.attributes so they are available at span creation time.
  • Updated the remaining heater capture calls to use span.setAttributes() with the returned attributes.

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)

How Has This Been Tested?

  • Existing unit tests (npm run test in @opentelemetry/instrumentation-http)
  • Lint check (npm run lint)

Checklist:

  • Followed the style guidelines of this project
  • Unit tests have been added
  • Documentation has been updated

…an creation time

Signed-off-by: Vitor Vasconcellos <vitor.vasconcellos@mercadolivre.com>
@codecov
Copy link
Copy Markdown

codecov Bot commented Feb 11, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.68%. Comparing base (4cd3644) to head (87fbb15).
⚠️ Report is 5 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #6396   +/-   ##
=======================================
  Coverage   95.68%   95.68%           
=======================================
  Files         364      364           
  Lines       11786    11786           
  Branches     2744     2744           
=======================================
  Hits        11278    11278           
  Misses        508      508           
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Signed-off-by: Vitor Vasconcellos <vitor.vasconcellos@mercadolivre.com>
Signed-off-by: Vitor Vasconcellos <vitor.vasconcellos@mercadolivre.com>
Copy link
Copy Markdown
Contributor

@trentm trentm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! Thanks for taking this. This LGTM, just the changelog comment.

Comment thread experimental/CHANGELOG.md Outdated
### :house: Internal

* perf(otlp-transformer): optimize toAnyValue performance [#6287](https://github.com/open-telemetry/opentelemetry-js/pull/6287) @AbhiPrasad
* chore(instrumentation-http): provide `http.request.header.<key>` at server span creation time [#6396](https://github.com/open-telemetry/opentelemetry-js/pull/6396) @vitorvasc
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd call this a "feat" (also update the PR title), and move this to the "Features" section of the changelog. Also, because I've been slow to review and there has been a release in the interim, you'll need to move this up to the "# Unreleased" section.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That makes sense, thanks!

Updated. 🙂

Signed-off-by: Vitor Vasconcellos <vitor.vasconcellos@mercadolivre.com>
Copilot AI review requested due to automatic review settings March 2, 2026 09:38
@vitorvasc vitorvasc changed the title chore(instrumentation-http): provide http.request.header.<key> at server span creation time feat(instrumentation-http): provide http.request.header.<key> at server span creation time Mar 2, 2026
Signed-off-by: Vitor Vasconcellos <vitor.vasconcellos@mercadolivre.com>
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Ensures http.request.header.<key> attributes for incoming (server) HTTP spans are available at span creation time, aligning with the HTTP semconv “SHOULD be provided at span creation time” guidance to support sampling decisions.

Changes:

  • Refactors headerCapture to return an Attributes object rather than mutating a span directly.
  • Moves server request header capture to occur before _startHttpSpan by merging captured headers into spanOptions.attributes.
  • Updates remaining header capture call sites to apply returned attributes via span.setAttributes(), and updates unit tests accordingly.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
experimental/packages/opentelemetry-instrumentation-http/test/functionals/utils.test.ts Updates tests to validate returned header attributes instead of mocking span.setAttribute.
experimental/packages/opentelemetry-instrumentation-http/src/utils.ts Refactors headerCapture to build and return an Attributes map.
experimental/packages/opentelemetry-instrumentation-http/src/http.ts Applies captured headers via setAttributes() and captures server request headers before span creation.
experimental/CHANGELOG.md Adds a changelog entry for the behavior change.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +639 to +644
Object.assign(
spanAttributes,
instrumentation._headerCapture.server.captureRequestHeaders(
header => request.headers[header]
)
);
Copy link

Copilot AI Mar 2, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

New behavior moves server request header capture into the span creation attributes so sampling can use http.request.header.*. There isn't a test that verifies these header attributes are visible to the sampler at startSpan time (e.g., via a custom Sampler's shouldSample receiving them). Consider adding a functional/unit test that installs a Sampler capturing the attributes argument and asserts the configured header keys/values are present when the server span is created.

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added: 87fbb15

… attribute capturing

Signed-off-by: Vitor Vasconcellos <vitor.vasconcellos@mercadolivre.com>
@trentm trentm added this pull request to the merge queue Mar 2, 2026
Merged via the queue into open-telemetry:main with commit 30f94fe Mar 2, 2026
27 checks passed
@vitorvasc vitorvasc deleted the instr-http_header-capture branch March 2, 2026 22:04
@otelbot-js otelbot-js Bot mentioned this pull request Mar 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

instr-http SHOULD provide http.request.header.<key> at server span creation time

3 participants